package com.tcwlkj.uuService.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;

import com.tcwlkj.uuService.R;

import java.util.List;

/**
 * Created by Administrator on 2017/10/20 0020.
 */

public class CommomDialog extends Dialog implements View.OnClickListener {

    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;
    private EditText editText;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private OnCancelsListener cancelsListener;
    private String positiveName;
    private String negativeName;

    private String title;
    private ImageView iv_work;

    public CommomDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public CommomDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener,OnCancelsListener cancelsListener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
        this.cancelsListener=cancelsListener;
    }

    protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public CommomDialog setTitle(String title){
        this.title = title;
        return this;
    }

    public CommomDialog setPositiveButton(String name){
        this.positiveName = name;
        return this;
    }
    public CommomDialog setTitleVisibility(Boolean boolea){
        if(boolea){
            contentTxt.setVisibility(View.VISIBLE);
        }else {
            contentTxt.setVisibility(View.GONE);
        }
        return this;
    }



    public CommomDialog setNegativeButton(String name){
        this.negativeName = name;
        return this;
    }
    public CommomDialog setvisibility(Boolean b){
        if(b){
            editText.setVisibility(View.VISIBLE);
        }else {
            editText.setVisibility(View.GONE);
        }
        return this;
    }

    public CommomDialog setimageVisibility(Boolean b){
        if(b){
            iv_work.setVisibility(View.VISIBLE);
        }else {
            iv_work.setVisibility(View.GONE);
        }
        return this;
    }

    public CommomDialog setTextColor(){
        submitTxt.setTextColor(getContext().getResources().getColor(R.color.light_blue));
        return this;
    }



    public CommomDialog settitleVisibility(Boolean b){
        if(b){
            contentTxt.setVisibility(View.VISIBLE);
        }else {
            contentTxt.setVisibility(View.GONE);
        }
        return this;
    }

    public CommomDialog setCancleVisibility(Boolean b){
        if(b){
            cancelTxt.setVisibility(View.VISIBLE);
        }else {
            cancelTxt.setVisibility(View.GONE);
        }
        return this;
    }

//    public CommomDialog setEdittext(String b){
//        editText.setHint(b);
//        return this;
//    }


    public String getEditContent(){
        return editText.getText().toString().trim();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_commom);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView(){
        contentTxt = (TextView)findViewById(R.id.content);
        titleTxt = (TextView)findViewById(R.id.title);
        submitTxt = (TextView)findViewById(R.id.submit);
        editText= (EditText) findViewById(R.id.et_content);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView)findViewById(R.id.cancel);
        iv_work = (ImageView) findViewById(R.id.iv_work);
        cancelTxt.setOnClickListener(this);

        contentTxt.setText(content);
        if(!TextUtils.isEmpty(positiveName)){
            submitTxt.setText(positiveName);
        }

        if(!TextUtils.isEmpty(negativeName)){
            cancelTxt.setText(negativeName);
        }

        if(!TextUtils.isEmpty(title)){
            titleTxt.setText(title);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel:
                this.dismiss();
                if(cancelsListener!=null){
                    cancelsListener.onClick(this,true);
                }
                break;
            case R.id.submit:
                this.dismiss();
                if(listener != null){
                    listener.onClick(this, true);
                }
                break;
        }
    }

    public interface OnCloseListener{
        void onClick(Dialog dialog, boolean confirm);
    }

    public interface OnCancelsListener{
        void onClick(Dialog dialog, boolean confirm);
    }
}
